<?php

/**
 * FILE:        refreshmessengertoken.php
 *
 * DESCRIPTION: This is the refresh token code required for using
 *              consent/delegation tokens with the Windows Live Messenger Web
 *              Toolkit on sites using PHP.
 *
 * EXTENSION LIBRARIES ARE REQUIRED, PLEASE CONFIGURE IN PHP.INI
 * - hash or mhash extension
 * - mcrypt extension
 * - curl extension
 *
 * Copyright (c) Microsoft Corporation.  All Rights Reserved.
 */

// Load common settings. For more information, see Settings.php.
include 'lib/settings.php';
include 'lib/windowslivelogin.php';

// The name of the consent token cookie
define('CONSENT_TOKEN_COOKIE_NAME', "msgr-consent-token");

// The name of the delegation token cookie
define('DELEGATION_TOKEN_COOKIE_NAME', "msgr-delegation-token");

// Make the key file a constant.
define('KEYFILE', $KEYFILE);

// Current consent token that is stored in the consent cookie
$consentToken = null;

// The Windows Live Login object.
$windowsLiveLogin = null;

// Tests whether the consent token cookie is available
function isConsentCookieAvailable()
{
    global $consentToken;
    
    // Check if consent token cookie is set and is not empty.
    if (isset($_COOKIE[CONSENT_TOKEN_COOKIE_NAME]) && 
        strlen($_COOKIE[CONSENT_TOKEN_COOKIE_NAME]) != 0)
    {
        $consentToken = urlencode($_COOKIE[CONSENT_TOKEN_COOKIE_NAME]);
        return true;
    }
    else
    {
        return false;
    }
}

// Checks if the delegation token cookie is present
function isDelegationTokenCookiePresent()
{
    global $consentToken;
    
    // Check if delegation token cookie is set and is not empty.
    if (isset($_COOKIE[DELEGATION_TOKEN_COOKIE_NAME]) && 
        strlen($_COOKIE[DELEGATION_TOKEN_COOKIE_NAME]) != 0)
    {
        $consentToken = urlencode($_COOKIE[DELEGATION_TOKEN_COOKIE_NAME]);
        return true;
    }
    else
    {
        return false;
    }
}

// Updates the cookies with a refreshed token
function updateDelegatedAuthenticationCookie($token)
{
    setrawcookie(DELEGATION_TOKEN_COOKIE_NAME, $token->getDelegationToken(), 0, "/");
    setrawcookie(CONSENT_TOKEN_COOKIE_NAME, $token->getToken(), 0, "/");
}

// Expires a given cookie.
function expireCookie($cookieName)
{
    // Set the cookie such that it is expired -1 years.
    setrawcookie($cookieName, "", time()-31536000, "/");
}

// Expires the delegation token cookie
function expireDelegatedAuthenticationCookie()
{
    expireCookie(DELEGATION_TOKEN_COOKIE_NAME);
}

// Refreshes the token
function refreshDelegatedAuthenticationCookie()
{
    global $windowsLiveLogin, $consentToken;
    $windowsLiveLogin = WindowsLiveLogin::initFromXml(KEYFILE);
    
    $token = $windowsLiveLogin->processConsentToken($consentToken);
    
    $expireDelegationToken = false;
    
    if ($token)
    {
        if (!$token->isValid())
        {            
            if ($token->refresh())
            {
                updateDelegatedAuthenticationCookie($token);
            }
            else
            {
                $expireDelegationToken = true;
            }
        }
        else if (!isDelegationTokenCookiePresent())
        {
            updateDelegatedAuthenticationCookie($token);
        }
    }
    
    if ($expireDelegationToken)
    {
        expireDelegatedAuthenticationCookie();
    }
}

// Main entrypoint
if (isConsentCookieAvailable())
{
    refreshDelegatedAuthenticationCookie();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
</body>
</html>